package com.sheng.service.user;

import com.sheng.dao.BaseDao;
import com.sheng.dao.user.UserDao;
import com.sheng.dao.user.UserDaoImpl;
import com.sheng.pojo.User;
import org.junit.Test;

import java.sql.Connection;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) throws Exception {
        Connection connection = null;
        User user = null;
        connection = BaseDao.getConnection();
        user = userDao.getLoginUser(connection, userCode);
        BaseDao.close(connection, null, null);
        return user;
    }

    @Override
    public boolean updatePwd(int id, String pwd) throws Exception {
        Connection connection = null;
        boolean flag = false;

        connection = BaseDao.getConnection();

        if (userDao.updatePwd(connection, id, pwd) > 0) {
            flag = true;
        }
        BaseDao.close(connection, null, null);
        return flag;
    }

    @Test
    public void test() {
        UserServiceImpl userService = new UserServiceImpl();
        try {
            User user = userService.login("admin", "123456");
            System.out.println(user.getUserName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
